package com.jiubodou.myoauth2.oauth2class.jiubodou;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;

public class JiuBoDouAuthenticationToken extends AbstractAuthenticationToken implements AuthenticatedPrincipal {
    private static final long serialVersionUID = 570L;
    private final Object principal;/*在未认证的时候，这个地方基本上都是 账号 例如：“1760518xxxx 或者 92866xxxx@qq.com 等等
    当认证完成之后 这里基本上就是一个 类似这样的对象
    ”*/
    private Object credentials;
    private String code;
    private String clientId;/*这里*/

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public JiuBoDouAuthenticationToken(Object principal, Object credentials, String code) {
        super((Collection)null);
        this.principal = principal;
        this.credentials = credentials;
        this.code=code;
        this.setAuthenticated(false);
    }

    public JiuBoDouAuthenticationToken(Object principal, Object credentials,String code, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        this.code=code;
        super.setAuthenticated(true);
    }

    public static JiuBoDouAuthenticationToken unauthenticated(Object principal, Object credentials,String code) {
        return new JiuBoDouAuthenticationToken(principal, credentials,code);
    }

    public static JiuBoDouAuthenticationToken authenticated(Object principal, Object credentials, String code,Collection<? extends GrantedAuthority> authorities) {
        return new JiuBoDouAuthenticationToken(principal, credentials, code,authorities);
    }

    public Object getCredentials() {
        return this.credentials;
    }

    public Object getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        Assert.isTrue(!isAuthenticated, "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        super.setAuthenticated(false);
    }

    public void eraseCredentials() {
        super.eraseCredentials();
        this.credentials = null;
    }

    @Override   /*直接走这个方法，不走父类的方法*/
    public String getName() {
        return clientId;
    }
}
